package com.zhida.jiyangcenter.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Keep;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhida.jiyangcenter.BusinessConstKt;

import chooongg.base.utils.ToastUtilsKt;


@Keep
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;
    private static WXPay wxPay;

    public static void setWxPay(WXPay wxPay) {
        WXPayEntryActivity.wxPay = wxPay;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, BusinessConstKt.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
                case 0:
                    wxPay.payBack(resp.errCode);
                    break;
                case -1:
                    ToastUtilsKt.toast(resp.errStr,null);
                    break;
                case -2:
                    ToastUtilsKt.toast("取消支付",null);
            }
        }
        finish();
        overridePendingTransition(0, 0);
    }
}